package com.itboye.eweilisten.me.timu;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.adapter.ScoreReparAdapter;
import com.itboye.eweilisten.adapter.ScoreTwoAdapter;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.me.home.ActivityScoreReport;
import com.itboye.eweilisten.model.YesCuoBean;
import com.itboye.eweilisten.presenter.UserPresenter;
import com.itboye.eweilisten.util.sputil.BaseUiListener;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.widget.GoodBean;
import com.itboye.eweilisten.util.widget.MyGridView;
import com.itboye.eweilisten.util.widget.ShanXinView;
import com.itboye.eweilisten.util.widget.ShanXinViewData;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 微测试成绩
 */
public class ChengJiActivity extends BaseActivity {
    ShanXinView shanxingview;
    private ArrayList<ShanXinViewData> histogramDatas;
    private MyGridView chenjigrid,chenjigrid_child;
    private ScoreReparAdapter adapter;
    private ScoreTwoAdapter adapter02;
    LinearLayout layout01,layout02;
    List<GoodBean> list = new ArrayList<GoodBean>();
    YesCuoBean bean;
    UserPresenter presenter;
    String unit_id;
    int yes = 0;
    int no = 0;
    int lou = 0;
    int zong = 0;
    private TextView tvTitleUimt;
    private TextView tvTimeJun;
    private ImageView back;
    private PopupWindow window;
    private TextView tvTitle;
    private TextView tvshare;
    private Tencent mTencent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheng_ji);
        mTencent = Tencent.createInstance(Const.APP_ID_QQ, this);
        bean = (YesCuoBean) getIntent().getSerializableExtra("bean");

        onViewCaner(bean);

        chenjigrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ChengJiActivity.this, CuoFenXiActivity.class);
                intent.putExtra("id", bean.getList().get(position).getId());
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", bean);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        //有chlid
        chenjigrid_child.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ChengJiActivity.this, CuoFenXiActivity.class);
                intent.putExtra("id", bean.getList().get(0).getChild().get(position).getId());
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", bean);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tvshare:
//                onShare();
                break;
        }
    }

    public void onViewCaner(YesCuoBean bean) {
        tvTitleUimt.setText(bean.getUnit_name());
        // tvTimeJun.setText("交卷时间:"+ TimeFormat.DateFormat(bean.getCreate_time()));
        // zong = bean.getWork().size();
        yes = Integer.parseInt(bean.getCorrect_cnt());
        no = Integer.parseInt(bean.getError_cnt());
        lou = Integer.parseInt(bean.getMissing_cnt());
        zong = yes + no + lou;

        if (bean.getList().get(0).getDt_type().equals("6227")){
            adapter02 = new ScoreTwoAdapter(this, bean.getList().get(0).getChild());
            chenjigrid_child.setAdapter(adapter02);
            layout01.setVisibility(View.GONE);
            layout02.setVisibility(View.VISIBLE);
        }else{
            layout01.setVisibility(View.VISIBLE);
            layout02.setVisibility(View.GONE);
            adapter = new ScoreReparAdapter(this, bean);
            chenjigrid.setAdapter(adapter);
        }
        histogramDatas = new ArrayList<ShanXinViewData>();
        Log.d("histog", yes + "\t" + no + "\t" + lou);

        histogramDatas.add(new ShanXinViewData(yes, yes + "/" + zong));
        if(no==0){
            histogramDatas.add(new ShanXinViewData(0,""));
        }else {
            histogramDatas.add(new ShanXinViewData(no, no + "/" + zong));
        }
        if (lou == 0) {
            histogramDatas.add(new ShanXinViewData(0, ""));
        } else {
            histogramDatas.add(new ShanXinViewData(lou, lou + "/" + zong));
        }
        shanxingview.setData(histogramDatas);//(v);

    }


    /**
     * 分享布局
     */
    @SuppressWarnings("deprecation")
    public void onShare() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.item_addd_share, null);
        window = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);

        window.setFocusable(true);
        window.setBackgroundDrawable(new BitmapDrawable());
        window.setAnimationStyle(R.style.popupwindow_anim_style);
        window.showAtLocation(findViewById(R.id.tvshare), Gravity.BOTTOM, 0, 0);
        TextView tvdinimis = (TextView) view.findViewById(R.id.tvdinimis);
        ImageView shareQQ = (ImageView) view
                .findViewById(R.id.shareQQ);
        ImageView shareWeiXinHaoYou = (ImageView) view
                .findViewById(R.id.shareWeiXinHaoYou);
        tvdinimis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });
        shareQQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickShareToQQ();//QQ分享
                window.dismiss();
            }
        });
        shareWeiXinHaoYou.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onPengYou();//微信好友分享
                window.dismiss();
            }
        });
        ImageView imgSharePengYouQuan = (ImageView) view
                .findViewById(R.id.imgSharePengYouQuan);
        imgSharePengYouQuan.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onPengYouQuan();//微信朋友圈分享
                window.dismiss();
            }
        });
    }

    private void onClickShareToQQ() {
        Bundle b = onQQ();
        if (b != null) {
            shareParams = b;
            Thread thread = new Thread(shareThread);
            thread.start();
        }
    }

    Bundle shareParams = null;
    Handler shareHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        }
    };

    // 线程类，该类使用匿名内部类的方式进行声明
    Runnable shareThread = new Runnable() {

        public void run() {
            doShareToQQ(shareParams);
            Message msg = shareHandler.obtainMessage();

            // 将Message对象加入到消息队列当中
            shareHandler.sendMessage(msg);

        }
    };

    private void doShareToQQ(Bundle params) {
        mTencent.shareToQQ(this, params, new BaseUiListener() {
            protected void doComplete(JSONObject values) {
//                showResult("shareToQQ:", "onComplete");
            }

            @Override
            public void onError(UiError e) {
//                showResult("shareToQQ:", "onError code:" + e.errorCode
//                        + ", msg:" + e.errorMessage + ", detail:"
//                        + e.errorDetail);
            }

            @Override
            public void onCancel() {
//                showResult("shareToQQ", "onCancel");
            }
        });
    }

    /**
     * qq分享
     */
    public Bundle onQQ() {
        Bundle bundle = new Bundle();
        bundle.putString("title", "易微听");
        bundle.putString("imageUrl", "http://api.ewelisten.com/index.php/picture/index?id=6");
        bundle.putString("targetUrl", "http://api.ewelisten.com/web.php/web/detail?id=8");
        bundle.putString("summary", "我在易微听发现了一个好玩的东西，速来围观");
        bundle.putString("site", "2222");
        bundle.putString("appName", "易微听");
        return bundle;
    }

    /**
     * 微信朋友圈分享
     */
    public void onPengYouQuan() {

        final IWXAPI api = WXAPIFactory.createWXAPI(this, Const.APP_ID, true);
        api.registerApp(Const.APP_ID);
//        api.openWXApp();
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = "http://api.ewelisten.com/web.php/web/detail?id=8";
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = "我在易微听发现了一个好玩的东西，速来围观";
        msg.description = "我在易微听发现了一个好玩的东西，速来围观";
//         Bitmap thumb = BitmapFactory.decodeResource(
//         getResources(), R.drawable.ic_moren);
//         msg.thumbData=thumb;
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "webpage";
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);

    }

    /**
     * 微信好友分享
     */
    public void onPengYou() {
        final IWXAPI api = WXAPIFactory.createWXAPI(this, Const.APP_ID, true);
        api.registerApp(Const.APP_ID);
        api.openWXApp();
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = "http://api.ewelisten.com/web.php/web/detail?id=8";
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = "易微听";
        msg.description = "我在易微听发现了一个好玩的东西，速来围观";

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "webpage";
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);
    }
}
